package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.FoOrder;
import com.fun_ordering.pojo.OrderDetail;
import com.fun_ordering.service.FoOrderService;
import com.fun_ordering.service.FoStatusService;
import com.fun_ordering.service.OrderDetailService;
import com.fun_ordering.util.ONGUtil;
import com.fun_ordering.util.WeChatPayUtil;
import com.fun_ordering.vo.OrderVo;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/foOrder")
public class FoOrderController {

	@Autowired
	private FoOrderService foOrderService;

	@Autowired
	private OrderDetailService orderDetailService;

	@Autowired
	private WeChatPayUtil weChatPayUtil;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<FoOrder> findAll(FoOrder foOrder){
		return foOrderService.findAll(foOrder);
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return foOrderService.findPage(page, rows);
	}

	/**
	 * 增加
	 * 添加订单信息
	 * @param
	 * @return
	 */
	@RequestMapping("/add")
	@Transactional //回退事务
	public ApiResponse add(@RequestBody OrderVo orderVo){
		try {
			//定义orderVo对象工具类，定义接受订单信息的数组
			FoOrder foOrder = new OrderVo();
			//将订单信息属性赋给满足条件的对象
			BeanUtils.copyProperties(orderVo,foOrder);
			//调用添加方法
			//补充商户订单号
			foOrder.setTradeNo(ONGUtil.generateOrderNumber());
			foOrderService.add(foOrder);

			for (int i = 0; i <orderVo.getDishIds().length; i++) {
				OrderDetail orderDetail = new OrderDetail();
				orderDetail.setOrderId(foOrder.getOrderId());
				orderDetail.setDishId(orderVo.getDishIds()[i]);
				orderDetail.setDishNumber(orderVo.getDishNumbers()[i]);
				orderDetail.setDishPrice(orderVo.getDishPrices()[i]);
				orderDetailService.add(orderDetail);
			}
			long total = (foOrder.getTotalPrice().multiply(new BigDecimal(100)).longValue());
			String des = "西安市高陵区顺意地摊鱼店-支付下单";
			PrepayWithRequestPaymentResponse jsapi = weChatPayUtil.jsapi(total, foOrder.getTradeNo(), des, foOrder.getCustomerId());
			System.out.println("---------------------支付了"+"/n"+jsapi);
			return ApiResponse.success(jsapi);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}
	/**
	 * 修改
	 * @param foOrder
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody FoOrder foOrder){
		try {
			foOrderService.update(foOrder);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/findOne")
	public FoOrder findOne(Integer orderId){
		return foOrderService.findOne(orderId);
	}

	/**
	 * 批量删除
	 * @param orderIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] orderIds){
		try {
			foOrderService.delete(orderIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param foOrder
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody FoOrder foOrder, int page, int rows  ){
		return foOrderService.findPage(foOrder, page, rows);
	}

	/**
	 * 确认出餐
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/orderAleardy")
	public ApiResponse orderAleardy(Integer orderId){
		try {
//			System.out.println("确认出餐");
			foOrderService.orderAleardy(orderId);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

}
